<template>
  <!-- 可以控制HTML片段指定在某一父节点下呈现/渲染，而不必诉诸全局状态或将其拆分为两个组件 -->
  <Teleport to="body">
    <transition name="zoom-fade" mode="out-in">
      <div class="gm-app-search-modal" @click.stop v-if="visible">
        <div class="gm-app-search-modal-content" v-click-out-side="handleClose">
          <div class="gm-app-search-modal-input__wrapper">
            <el-input
              class="gm-app-search-modal-input"
              v-model="searchContent"
              :placeholder="t('common.searchText')"
              ref="inputRef"
              size="large"
              clearable
              @input="handleSearch"
            >
              <template #prefix>
                <svg-icon iconClass icon-class="search" class="el-input__icon" />
              </template>
            </el-input>
            <span
              class="gm-app-search-modal-cancel"
              @click="handleClose"
            >{{ t('common.cancelText') }}</span>
          </div>

          <div
            class="gm-app-search-modal-not-data"
            v-show="getIsNotData"
          >{{ t('component.app.searchNotData') }}</div>

          <ul class="gm-app-search-modal-list" v-show="!getIsNotData" ref="scrollWrap">
            <li
              :refs="setRefs(index)"
              v-for="(item, index) in searchResult"
              :key="item.path"
              :data-index="index"
              :class="['gm-app-search-modal-list__item', {
                ['gm-app-search-modal-list__item--active']: activeIndex === index
              }]"
              @mouseenter="handleMouseenter"
              @click="handleEnter"
            >
              <div :class="'gm-app-search-modal-list__item-icon'">
                <svg-icon :icon-class="item.icon || 'form-select'"  :class-name="'svg-class'" :size="20"/>
              </div>
              <div :class="'gm-app-search-modal-list__item-text'">{{ item.name }}</div>
              <div :class="'gm-app-search-modal-list__item-enter'">
                <svg-icon icon-class="enter" :size="20" />
              </div>
            </li>
          </ul>
          <AppSearchFooter />
        </div>
      </div>
    </transition>
  </Teleport>
</template>

<script lang="ts" setup>
import { useRefs } from '@/hooks/core/useRefs'
import { computed, ref, unref } from 'vue'
import AppSearchFooter from './AppSearchFooter.vue'
import { useI18n } from '@/hooks/web/useI18n'
import vClickOutSide from '@/directives/clickOutside'
import { useMenuSearch } from './useMenuSearch'

const props = defineProps({
  visible: { type: Boolean }
})
const { t } = useI18n()

const scrollWrap = ref(null)
const emit = defineEmits(['close'])
const [refs, setRefs] = useRefs()

const getIsNotData = computed(
  () => !keyword || unref(searchResult).length === 0
)
const searchContent = ref('')

const {
  handleSearch,
  searchResult,
  keyword,
  activeIndex,
  handleEnter,
  handleMouseenter
} = useMenuSearch(refs, scrollWrap, emit)

function handleClose() {
  searchResult.value = []
  emit('close')
}
</script>
<style lang="scss" scoped>
.gm-app-search-modal {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 800;
  display: flex;
  width: 100%;
  height: 100%;
  padding-top: 50px;
  background-color: rgba(0, 0, 0, 0.25);
  justify-content: center;

  &-cancel {
    // display: inline-block;
    display: none;
  }

  &-content {
    position: relative;
    width: 632px;
    margin: 0 auto auto;
    background-color: #fff;
    border-radius: 16px;
    box-shadow: 0 25px 50px -12px rgb(0 0 0 / 25%);
    flex-direction: column;
  }

  &-input__wrapper {
    display: flex;
    padding: 14px 14px 0;
    justify-content: space-between;
    align-items: center;
  }

  &-input {
    width: 100%;
    font-size: 1.2em;
    color: #1c1e21;
    border-radius: 6px;
  }
  &-not-data {
    display: flex;
    width: 100%;
    height: 100px;
    font-size: 0.9;
    color: #969faf;
    align-items: center;
    justify-content: center;
  }

  :deep(.el-input__inner) {
    font-size: 14px;
    margin-left: 5px;
  }

  &-list {
    max-height: 472px;
    padding: 0 14px;
    padding-bottom: 20px;
    margin: 0 auto;
    margin-top: 14px;
    overflow: auto;

    &__item {
      position: relative;
      display: flex;
      height: 56px;
      padding-bottom: 4px;
      padding-left: 14px;
      margin-top: 8px;
      font-size: 14px;
      color: rgba(0, 0, 0, 0.85);
      cursor: pointer;
      background-color: #fff;
      border-radius: 4px;
      box-shadow: 0 1px 3px 0 #d4d9e1;
      align-items: center;

      > div:first-child,
      > div:last-child {
        display: flex;
        align-items: center;
      }
      &-icon {
        width: 30px;
      }
      &-text {
        flex: 1;
      }
      &-enter {
        width: 30px;
      }
    }

    &__item--active {
      color: #fff;
      background-color: #0960bd;
      &-enter {
        opacity: 100%;
      }
    }
  }
}
.svg-class{}
</style>
